package com.blue.excel.util;

import com.blue.excel.model.CellStyleInfo;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by ChenYaHui on 2017/8/8.
 */
public class CellStyleMaker {

    Workbook workbook;

    Set<CellStyle> cellStyles = new HashSet<>();

    public CellStyleMaker(Workbook workbook) {
        this.workbook = workbook;
    }

    public CellStyle getCellStyle(CellStyleInfo cellStyleInfo){
        for(CellStyle cellStyle : cellStyles){
            if(cellStyleInfo.match(cellStyle)){
                return cellStyle;
            }
        }

        CellStyle cellStyle = createCellStyle(cellStyleInfo);
        cellStyles.add(cellStyle);
        return cellStyle;
    }

    private CellStyle createCellStyle(CellStyleInfo cellStyleInfo){
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFillForegroundColor(cellStyleInfo.fillForegroundColor);
        cellStyle.setFillPattern(cellStyleInfo.fillPattern);
        cellStyle.setBorderTop(cellStyleInfo.borderTop);
        cellStyle.setBorderRight(cellStyleInfo.borderRight);
        cellStyle.setBorderBottom(cellStyleInfo.borderBottom);
        cellStyle.setBorderLeft(cellStyleInfo.borderLeft);
        cellStyle.setVerticalAlignment(cellStyleInfo.verticalAlignment);
        cellStyle.setAlignment(cellStyleInfo.alignment);
        cellStyle.setDataFormat(cellStyleInfo.dataFormat);
        return cellStyle;
    }

    public CellStyle getCellStyle(CellStyle cellStyle){
        return getCellStyle(new CellStyleInfo(cellStyle));
    }

}
